<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="68px">
			<el-form-item label="订单编号" prop="orderNumber">
				<el-input v-model="queryParams.orderNumber" placeholder="请输入订单编号" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="市场" prop="courseDealerId">
				<el-select v-model="queryParams.courseDealerId" placeholder="请选择市场" clearable filterable>
					<el-option v-for="(item, index) in dealerlist" :key="index" :label="item.name"
						:value="item.id"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="联系人" prop="userName">
				<el-input v-model="queryParams.userName" placeholder="请输入联系人" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="联系电话" prop="userPhone">
				<el-input v-model="queryParams.userPhone" placeholder="请输入联系电话" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
					v-hasPermi="['complaint:order:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
					@click="handleDelete" v-hasPermi="['complaint:order:remove']">删除</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<!-- <el-table-column label="主键" align="center" prop="id" />
			<el-table-column label="用户id" align="center" prop="userId" /> -->
			<el-table-column label="订单编号1" align="center" prop="orderNumber" />
			<el-table-column label="市场" align="center" prop="courseDealerName" width="80" />
			<el-table-column label="联系人" align="center" prop="userName" width="100" />
			<el-table-column label="联系电话" align="center" prop="userPhone" width="120" />
			<el-table-column label="申请原因" align="center" prop="type" />
			<el-table-column label="描述" align="center">
				<template slot-scope="scope">
					<div class="content">
						{{ scope.row.content }}
					</div>
				</template>
			</el-table-column>
			<el-table-column label="凭证" align="center">
				<template slot-scope="scope">
					<div style="display: flex;justify-content: center;">
						<el-image style="width: 70px; height: 70px;margin: 0px 5px;"
							v-for="(voucher, i) in scope.row.voucherList" :key="i" :src="voucher" fit="contain"
							:preview-src-list="scope.row.voucherList"></el-image>
					</div>
				</template>
			</el-table-column>
			<el-table-column label="快递单号(市场)" align="center" prop="mailNumberDealer" />
			<el-table-column label="状态" align="center">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.complaint_order_status" :value="scope.row.status" />
				</template>
			</el-table-column>
			<el-table-column label="申请时间" align="center" prop="createTime" />
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-document" @click="examine(scope.row.id)"
						v-hasPermi="['complaint:order:edit']" v-if="scope.row.status == 0">审核</el-button>
					<el-button size="mini" type="text" icon="el-icon-view" @click="jump(scope.row.id)"
						v-hasPermi="['complaint:order:query']">详情</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 添加/修改 -->
		<el-dialog :title="title" :visible.sync="openOrder" width="80%" append-to-body>
			<el-form ref="form" :model="form" :rules="rulesOrder" label-width="80px">
				<el-form-item label="市场" prop="courseDealerId">
					<el-select v-model="form.courseDealerId" placeholder="请选择市场" filterable style="width: 100%;" @change="changeDealer">
						<el-option v-for="(item, index) in dealerlist" :key="index" :label="item.name" :value="item.id"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="联系人" prop="userName">
					<el-input v-model="form.userName" placeholder="请输入联系人"></el-input>
				</el-form-item>
				<el-form-item label="联系电话" prop="userPhone">
					<el-input v-model="form.userPhone" placeholder="请输入联系电话"></el-input>
				</el-form-item>
				<el-form-item label="申请原因" prop="type">
					<el-select v-model="form.type" multiple :multiple-limit="3" placeholder="请选择">
						<el-option v-for="item in goodsTypeList" :key="item.value" :label="item.label" :value="item.value"></el-option>
					</el-select>
				</el-form-item>
			</el-form>
		</el-dialog>

		<!-- 审核 -->
		<el-dialog title="审核" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" label-width="80px">
				<el-form-item label="审核" prop="status">
					<el-radio-group v-model="form.status">
						<el-radio :label="1">同意</el-radio>
						<el-radio :label="4">拒绝</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="是否寄回" prop="isSendBack" v-if="form.status == 1">
					<el-radio-group v-model="form.isSendBack">
						<el-radio :label="1">是</el-radio>
						<el-radio :label="2">否</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="快递单号" prop="mailNumber" v-if="form.status == 1 && form.isSendBack == 2">
					<el-input type="input" v-model="form.mailNumber" placeholder="请输入快递单号" />
				</el-form-item>
				<el-form-item label="拒绝原因" prop="msg" v-if="form.status == 4">
					<el-input type="textarea" :autosize="{ minRows: 5, maxRows: 10}" v-model="form.msg"
						placeholder="请输入拒绝原因" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="open = false">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listOrder,
		getOrder,
		delOrder,
		addOrder,
		updateOrder,
		getGoodsComplaintType
	} from "@/api/complaint/order";
	import {
		listDealer
	} from "@/api/course/dealer"
	export default {
		name: "Order",
		dicts: ['complaint_order_status'],
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				dealerlist: [],
				// 客诉订单表格数据
				orderList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				openOrder: false,
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					userId: null,
					orderNumber: null,
					courseDealerId: null,
					userName: null,
					userPhone: null,
					type: null,
					describe: null,
					voucher: null,
					status: null,
					isDel: null,
				},
				// 表单参数
				form: {
					id: 0,
					status: 0,
					isSendBack: 0,
					mailNumber: '',
					msg: '',
				},
				// 表单校验
				rulesOrder: {},
				goodsTypeList: []
			};
		},
		created() {
			this.getList();
			this.getGoodsType();

			listDealer().then(res => {
				if (res.code == 200) {
					this.dealerlist = res.rows;
				}
			});
		},
		methods: {
			/**
			 * 问题类型
			 */
			getGoodsType() {
				this.goodsTypeList = [];
				getGoodsComplaintType().then(res => {
					console.log('问题类型', res);
					if(res.code == 200){
						res.data.forEach(item => {
							this.goodsTypeList.push({
								value: item.dictValue,
								label: item.dictValue
							});
						});
					}
				})
			},
			/**
			 * 跳转详情
			 * @param {Object} id
			 */
			jump(id) {
				this.$router.push(`/complaint/orders/query/${id}`);
			},
			/** 查询客诉订单列表 */
			getList() {
				this.loading = true;
				listOrder(this.queryParams).then(response => {
					let list = [];
					response.rows.forEach(item => {
						item.voucherList = item.voucherList ? item.voucherList.map(item => item.url) : [];
						item.voucherList.map(img => {
							if (img.includes("bpm.app.edongfanghong.com")) {
								img.replace('http', 'https');
							}
						})
						list.push(item);
					});

					this.orderList = list;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					id: null,
					userId: null,
					orderNumber: null,
					courseDealerId: null,
					userName: null,
					userPhone: null,
					type: [],
					describe: null,
					voucher: null,
					status: null,
					isDel: null,
					createBy: null,
					createTime: null,
					updateBy: null,
					updateTime: null
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.id)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/**
			 * 审核
			 */
			examine(id) {
				this.form.id = id;
				this.form.status = 1;
				this.form.isSendBack = 1;
				this.form.mailNumber = '';
				this.open = true;
			},
			/**
			 * 提交审核
			 */
			submitForm() {
				if (this.form.status == 0) {
					this.$modal.msgError("请选择审核状态！");
				} else if(this.form.status == 1 && this.form.isSendBack == 2 && !this.form.mailNumber) {
					this.$modal.msgError("请输入快递单号！");
				} else if (this.form.status == 4 && !this.form.msg) {
					this.$modal.msgError("请输入拒绝原因！");
				} else {
					updateOrder({
						id: this.form.id,
						status: this.form.status,
						isSendBack: this.form.status == 1 ? this.form.isSendBack : 1,
						mailNumber: this.form.status == 1 && this.form.isSendBack == 2 ? this.form.mailNumber : '',
						msg: this.form.status == 4 ? this.form.msg : ''
					}).then(res => {
						this.open = false;
						if (res.code == 200) {
							this.$modal.msgSuccess("审核成功");
							this.getList();
						} else {
							this.$modal.msgError(res.msg);
						}
					});
				}
			},
			/**
			 * 添加订单
			 */
			handleAdd() {
				this.reset();
				this.openOrder = true;
				this.title = "添加客诉-订单";
			},
			/**
			 * 选择市场
			 * @param {Object} e
			 */
			changeDealer(e) {
				console.log('市场修改', e);
				let dealer = this.dealerlist.filter(item => item.id == e);
				this.form.userName = dealer[0].userName;
				this.form.userPhone = dealer[0].userPhone;
			},
			/**
			 * 删除按钮操作
			 */
			handleDelete(row) {
				const ids = row.id || this.ids;
				this.$modal.confirm('是否确认删除客诉-订单编号为"' + ids + '"的数据项？').then(function() {
					return delOrder(ids);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.content {
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
		overflow: hidden;
		text-overflow: ellipsis;
		max-height: 3.0em;
		/* 设置为行高的两倍 */
		line-height: 1.5em;
		/* 这里的行高应与下面p标签中的行高一致 */
	}
	
	::v-deep {
		.el-select {
			width: 100%;
		}
	}
</style>